package palmtop.com.palmtop.activity;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v4.view.LayoutInflaterFactory;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import palmtop.com.palmtop.R;

public class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    Toolbar mToolbar = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LayoutInflaterCompat.setFactory(LayoutInflater.from(this), new LayoutInflaterFactory() {
            @Override
            public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
                return null;
            }
        });
        super.onCreate(savedInstanceState);
        //super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        setContentView(layoutResID,-1,1);
    }

    public void setContentView(int layoutResID, int titleResId, int mode ){
        String title = "";
        if(titleResId>0){
            title = getString(titleResId);
        }
        setContentView(layoutResID,title,mode);
    }

    public void setContentView(int layoutResID,String title,int mode){
        super.setContentView(layoutResID);
        setUpToolBar(title,mode);
    }
    public void setUpToolBar(String title,int mode){
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if(mToolbar==null){
            return;
        }
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(title);
        if(mode == -1){
            return;
        }
        actionBar.setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNavigationClicked();
            }
        });
    }
    protected void onNavigationClicked(){
        finish();
    }
    @Override
    public void onClick(View view) {

    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }
}
